﻿//-----------------------------------------------------------------------
// Source File: "FolderPair.cs"
// Create Date: 07/30/2014 10:14 AM
// Last Updated: 07/30/2014 10:14 AM
// Authors(s): nealbailey@hotmail.com
//-----------------------------------------------------------------------
// GNU GENERAL PUBLIC LICENSE
//-----------------------------------------------------------------------
// Revision: Version 3, 29 June 2007
// Copyright © 2007 Free Software Foundation, Inc. http://fsf.org/
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  REFERENCE: http://www.gnu.org/licenses/
//-----------------------------------------------------------------------
// Copyright (c) 2010-2014 Baileysoft Solutions
//-----------------------------------------------------------------------
namespace Baileysoft.Gaming.Utils.Yasm
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Encapsulation object for working with user-defined folder pairs
    /// </summary>
    public class FolderPair : IDirectoryClonerArgs
    {
        #region Constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        public FolderPair()
            : this(string.Empty, string.Empty)
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="sourceFolder">The source folder</param>
        /// <param name="destinationFolder">The destination folder</param>
        public FolderPair(string sourceFolder, string destinationFolder)
        {
            Delimiter = "|";
            SourceFolder = new SourceFolder(sourceFolder);
            DestinationFolder = Path.Combine(destinationFolder,
                                             SourceFolder.FolderName);
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// The delimiter character to use when converting a <see cref="FolderPair"/> to a string
        /// </summary>
        public virtual string Delimiter
        {
            get; set;
        }

        /// <summary>
        /// The destination folder
        /// </summary>
        public virtual string DestinationFolder
        {
            get; set;
        }

        /// <summary>
        /// The <see cref="SourceFolder"/>
        /// </summary>
        public virtual SourceFolder SourceFolder
        {
            get; set;
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Get the available free space in the destination source path.
        /// </summary>
        /// <returns>A user friendly free-space <see cref="string"/></returns>
        /// <exception cref="System.ArgumentException">The exception that is thrown when one of the arguments provided to a method is not valid.</exception>
        /// <exception cref="System.ArgumentNullException">The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.</exception>
        /// <exception cref="System.FormatException">The exception that is thrown when the format of an argument does not meet the parameter specifications of the invoked method.</exception>
        public virtual string GetDestinationDriveFreeSpace()
        {
            var root = Path.GetPathRoot(DestinationFolder);
            var di = new DriveInfo(root);
            return SourceFolder.GetReadableFileSize(di.TotalFreeSpace);
        }

        /// <summary>
        /// Get the available free space in the source source path.
        /// </summary>
        /// <returns>A user friendly free-space <see cref="string"/></returns>
        /// <exception cref="System.ArgumentException">The exception that is thrown when one of the arguments provided to a method is not valid.</exception>
        /// <exception cref="System.ArgumentNullException">The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.</exception>
        /// <exception cref="System.FormatException">The exception that is thrown when the format of an argument does not meet the parameter specifications of the invoked method.</exception>
        public virtual string GetSourceDriveFreeSpace()
        {
            var root = Path.GetPathRoot(SourceFolder.Path);
            var di = new DriveInfo(root);
            return SourceFolder.GetReadableFileSize(di.TotalFreeSpace);
        }

        /// <summary>
        /// Converts the folder-pairs setting string into key-value pair based on the user defined delimiter string
        /// </summary>
        /// <returns>A key-value dictionary of folder pairs</returns>
        /// <exception cref="System.NullReferenceException">Parameter is null.</exception>
        /// <exception cref="System.ArgumentException">options is not one of the <see cref="T:System.StringSplitOptions" /> values.</exception>
        public virtual KeyValuePair<string, string> ToKeyValuePair(string folderPairString, string delimiter)
        {
            if (string.IsNullOrEmpty(folderPairString))
                return new KeyValuePair<string, string>();

            if (string.IsNullOrEmpty(Delimiter))
                Delimiter = "|";

            var arr = folderPairString.Split(new string[] {Delimiter}, StringSplitOptions.RemoveEmptyEntries);
            if (arr.Length < 2)
                return new KeyValuePair<string, string>();

            SourceFolder = new SourceFolder(arr[0]);
            DestinationFolder = arr[1];

            return ToKeyValuePair();
        }

        /// <summary>
        /// Converts the folder-pairs setting string into key-value pair based on the user defined delimiter string
        /// </summary>
        /// <returns>A key-value dictionary of folder pairs</returns>
        /// <exception cref="System.NullReferenceException">Parameter is null.</exception>
        /// <exception cref="System.ArgumentException">options is not one of the <see cref="T:System.StringSplitOptions" /> values.</exception>
        public virtual KeyValuePair<string, string> ToKeyValuePair()
        {
            if (string.IsNullOrEmpty(SourceFolder.Path) ||
                string.IsNullOrEmpty(DestinationFolder) ||
                string.IsNullOrEmpty(Delimiter))
            {
                return new KeyValuePair<string, string>();
            }

            return new KeyValuePair<string, string>(SourceFolder.Path, DestinationFolder);
        }

        /// <summary>
        /// Override ToString
        /// </summary>
        /// <returns><see cref="string"/></returns>
        public override string ToString()
        {
            if (string.IsNullOrEmpty(SourceFolder.Path) ||
                string.IsNullOrEmpty(DestinationFolder))
            {
                return base.ToString();
            }

            if (string.IsNullOrEmpty(Delimiter))
                Delimiter = "|";

            return string.Format("{0}{1}{2}", SourceFolder.Path.Trim(),
                                              Delimiter,
                                              DestinationFolder.Trim());
        }

        #endregion Methods
    }
}